//用户模块
export default {
    namespaced:true,
    state(){
        return{
            profile:{
              id: "",                    // 数据库中的用户ID
              phone_number: "",
              password: "",
              islogin: false,//登录状态
              token: '',
              cookies:'',
              // 用户权益信息
              totalTokens: 0,            // 剩余token数量
              totalValidityDays: 0,      // 剩余有效期天数
              expiresAt: null,           // 到期时间
            },
        }
    },
    mutations:{
        register(state, provider) {
            state.profile.islogin = true;
            state.profile.phone_number = provider
            console.log(state)
        },
        login(state, userData) {
            state.profile.islogin = true;
            state.profile.id = userData.id || userData.userId; // 设置数据库中的用户ID
            state.profile.phone_number = userData.phone_number;
            state.profile.token = userData.token;
        },
        logout(state){
            state.profile.islogin = false;
            state.profile.id = '';
            state.profile.phone_number = '';
            state.profile.token = '';
        },
        updateBenefit(state, benefitData) {
            // 更新用户权益信息
            if (benefitData.totalTokens !== undefined) {
                state.profile.totalTokens = benefitData.totalTokens;
            }
            if (benefitData.totalValidityDays !== undefined) {
                state.profile.totalValidityDays = benefitData.totalValidityDays;
            }
            if (benefitData.expiresAt !== undefined) {
                state.profile.expiresAt = benefitData.expiresAt;
            }
            console.log('用户权益已更新:', benefitData);
        },
        
        // 更新用户ID
        updateUserId(state, userId) {
            state.profile.id = userId;
            console.log('用户ID已更新:', userId);
        },
        
        // 累加权益（用于购买商品时）
        addBenefit(state, newBenefit) {
            // 处理token数量
            if (newBenefit.token !== undefined) {
                if (newBenefit.token === -1) {
                    // 无限制token
                    state.profile.totalTokens = -1;
                } else if (state.profile.totalTokens !== -1) {
                    // 累加token（如果当前不是无限制）
                    state.profile.totalTokens = (state.profile.totalTokens || 0) + newBenefit.token;
                }
            }

            // 处理有效期天数
            if (newBenefit.validityDays !== undefined) {
                if (newBenefit.validityDays === -1) {
                    // 永久有效
                    state.profile.totalValidityDays = -1;
                    state.profile.expiresAt = null;
                } else if (state.profile.totalValidityDays !== -1) {
                    // 累加有效期天数（如果当前不是永久）
                    state.profile.totalValidityDays = (state.profile.totalValidityDays || 0) + newBenefit.validityDays;

                    // 更新到期时间（基于累加后的总天数）
                    const newExpiresAt = new Date(Date.now() + state.profile.totalValidityDays * 24 * 60 * 60 * 1000);
                    state.profile.expiresAt = newExpiresAt.toISOString();
                }
            }

            console.log('权益已累加:', newBenefit, '当前权益:', {
                totalTokens: state.profile.totalTokens,
                totalValidityDays: state.profile.totalValidityDays,
                expiresAt: state.profile.expiresAt
            });
        }
    },
};